import request from '../../utils/request'
// pages/index/index.js

let allTopList = [];// 存放所有排行榜完整数据
let currentPage = 0;// 排行榜swiper当前所处的current

Page({
  /**
   * 页面的初始数据
   */
  data: {
    recommendList: [], //推荐歌单
    topList: [], //排行榜
    musicListId: '',// 当前播放歌单id
    isPlay: false,// 是否播放
    isStartShow: true,// 是否显示开屏动画
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    wx.hideTabBar();
    let recommendListData = await request('/personalized',{limit: 10});
    this.setData({
      recommendList: recommendListData.result
    })
    // 获取排行榜列表
    let toplist = await request('/toplist')
    toplist.list.splice(5)
    toplist = toplist.list
    // 根据排行榜的id通过查询歌单查询排行榜详情
    // 查询5个排行榜，循环五次
    let index = 0
    let listItem = {};
    let topList = [];
    while (index < 5) {
      let topListData = await request('/playlist/detail', { id: toplist[index].id })
      allTopList.push(topListData.playlist.tracks)
      listItem = { name: topListData.playlist.name, id: topListData.playlist.id, tracks: topListData.playlist.tracks.slice(0, 3) }
      index++;
      topList.push(listItem)
      this.setData({
        topList,
      })
      if (this.data.isStartShow == true) {
        this.setData({
          isStartShow: false
        })
        wx.showTabBar({
          animation: true
        })
      }
    }
  },

    // 跳转至每日推荐页面
    goToRecommend() {
      wx.navigateTo({
        url: '/package_4/pages/recommendSong/recommendSong',
      });
    },

      // 跳转至点击歌曲的详情页面/* pages/search/search.wxss */
  async toSongDetail(event) {
    let { song } = event.currentTarget.dataset;
    wx.setStorageSync('playingMusicList', allTopList[currentPage])
    wx.navigateTo({
      url: '/package_5/pages/songDetail/songDetail?song=' + JSON.stringify(song)
    })
  },


  // 跳转至音乐列表页面
  toMusicList(e) {

    wx.navigateTo({
      url: '/package_5/pages/musicList/musicList?musiclistid=' + e.currentTarget.dataset.musiclistid
    })
  },

  // 跳转至搜索页面
  toSearch() {
    wx.navigateTo({
      url: '/package_6/pages/search/search'
    })
  },

  // 跳转至歌单广场页面
  goToMusicListSquare() {
    wx.navigateTo({
      url: '/package_5/pages/musicListSquare/musicListSquare'
    })
  },

  // 获取排行榜当前所处的页数
  getCurrentPage(e) {
    // console.log(e);
    currentPage = e.detail.current
  },

  // 跳转至推荐歌单页面
  getMoreRecommendMusicList() {
    wx.navigateTo({
      url: '/package_4/pages/recommendMusicList/recommendMusicList'
    })
  },

  // 跳转至排行榜页面
  goToRanking() {
    wx.navigateTo({
      url: '/package_3/pages/ranking/ranking'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setTabBarItem
  },

  onTabItemTap(){
    let index = Math.floor(Math.random()*20 ) + 1;
    let path = '/static/images/asoul/1/' + index + '.png';

    const fs = wx.getFileSystemManager()
    // 判断文件/目录是否存在
    fs.access({
      path: `${path}`,
      success(res) {
      },
      fail(res) {
        // 文件不存在或其他错误
        path = '/static/images/asoul/1/2.png'
      }
    })
    // 同步接口
    try {
      fs.accessSync(`${path}`)
    } catch(e) {
      path = '/static/images/asoul/1/2.png'
      console.error(e)
    }

    wx.setTabBarItem({
      index: 0,
      text: '主页',
      iconPath: "/static/images/asoul/1/1.png",
      selectedIconPath: path
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})